import storage from "@/storage/storage";

export default {
    state: {
        // 存储token
        token: "",
        user: "",
        showLogin: false,
        role: "" // 可选
    },

    getters: {
        getToken(state) {
            return state.token || storage.get("token") || "";
        },
        getUser(state) {
            return state.user
        },
        getShowLogin(state) {
            return state.showLogin
        }
    },

    mutations: {
        // 修改token，并将token存入localStorage
        setToken(state, token) {
            state.token = token;
            storage.set('token', token);
        },
        delToken(state) {
            state.token = "";
            storage.remove("token");
            storage.remove('user');
            state.showLogin = true;
        },
        // 可选
        setUserRole(state, role) {
            state.role = role;
            storage.set('role', role);
            console.log('用户权限保存成功');
        },
        setUser(state, data) {
            state.user = data;
            storage.set('user', data);
        },
        setRole(state,data){
            state.role = data;
            storage.set('role',data);
        },
        setShowLogin(state, data) {
            state.showLogin = data;
            storage.set('showLogin', data);
        }
    },

    actions: {
        // removeToken: (context) => {
        // context.commit('setToken')
        // }
        setUser({commit}, data) {
            commit('setUser', data);
        },
        setShowLogin({commit}, data) {
            commit('setShowLogin', data);
        },
        setRole({commit},data){
            commit('setRole',data);
        }
    },
}